<?php

namespace App\Jobs;

use App\Models\FamilyEdu;
use App\Models\InterviewRemind;
use App\Models\TempPushRecord;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;

class EduJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $ids;
    public $tries = 0;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($ids)
    {
        $this->ids = $ids;
        $this->onQueue('edu_notice');
        $this->onConnection('redis');
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $ids = $this->ids;
        $models = FamilyEdu::query()
            ->select(DB::raw('ANY_VALUE(id) as id,ANY_VALUE(family_id) as family_id,ANY_VALUE(article_id) as article_id,ANY_VALUE(family_type_id) as family_type_id,user_id'))
            ->whereIn('family_id', $ids)
            ->groupBy('user_id')
            ->get();
        if ($models->isNotEmpty()) {
            //TempPushRecord::upsert($data,['user_id','temp_id']);

            foreach ($models as $model) {
                $tempPushRecord = TempPushRecord::createTemp($model->user_id, 	5);
                TempPush::dispatch($tempPushRecord, $model);
            }
        }else{
            $this->fail('数据为空id全部不存在');
        }
    }
}
